/**
 * Player Class, defines an object that has the attributes of a player.
 * 
 * @author ggere
 */

import java.awt.*;

class Player extends Actor {
	private String name;
	private InputManager inputManager;
	
	// variables to store our current position with more accuracy than the int's from the Sprite class
	private double dx;
	private double dy;
	
	// ship attributes
	private int speed;
	private int size;    

	/**
	 * Class constructor, specify the player name.
	 * 
	 * @param name the players name
	 * @param anim the animation for the player
	 */
	Player(String name, Animation anim, InputManager inputManager) {
		super(anim);		
		this.inputManager = inputManager;
		
		dx = 320;
		dy = 240;
		
		x = (int)(dx + .5);
		y = (int)(dy + .5);
		
		this.name = name;
		
		speed = 300;
		size = 5;
	}
	
	public String getName() {
		return name;
	}
	
	public void moveDown(double elapsedTime) {
		double dyNew = dy + speed * (elapsedTime /1000000000L);
		if (dyNew + getHeight() < 480) {
			dy = dyNew;
			y = (int)(dy + .5);
		}
	}

	public void moveUp(double elapsedTime) {
		double dyNew = dy - speed * (elapsedTime /1000000000L);
		if (dyNew > 0) {
			dy = dyNew;
			y = (int)(dy + .5);
		}
	}
	
	public void moveLeft(double elapsedTime) {
		double dxNew = dx - speed * (elapsedTime /1000000000L);
		if (dxNew > 0) {
			dx = dxNew;
			x = (int)(dx + .5);
		}
	}
	
	public void moveRight(double elapsedTime) {
		double dxNew = dx + speed * (elapsedTime /1000000000L);
		if (dxNew + getWidth() < 640) {
			dx = dxNew;
			x = (int)(dx + .5);			
		}
	}
		
    public void update(double elapsedTime) {
    	// update the players animation
        super.update(elapsedTime);
        
        //TODO: check for combinations of directions and modify the amount moved
        //      so that diagonals aren't faster that single directions.
       
        // move the ship
        if (inputManager.poll(name, InputManager.EVENT_LEFT)) {
			moveLeft(elapsedTime);
		}
		if (inputManager.poll(name, InputManager.EVENT_RIGHT)) {
			moveRight(elapsedTime);
		}
		if (inputManager.poll(name, InputManager.EVENT_UP)) {
			moveUp(elapsedTime);
		}
		if (inputManager.poll(name, InputManager.EVENT_DOWN)) {
			moveDown(elapsedTime);
		}
        
    }
	
    public void draw(Graphics g) {
    	g.drawImage(getImage(), x, y, null);
    }
	
}